package com.symaster.common.connect.util;

import com.alibaba.fastjson.JSONObject;
import com.symaster.common.connect.local.ConnectObj;
import com.symaster.common.connect.v2.MasterEnum;

import java.io.IOException;

/**
 * @author yinmiao
 * @version 2022-05-03 20:01
 */
public class ClientCommon implements MasterEnum {

    public static int byte2int(byte[] bytes) {
        return (bytes[0] & 0xFF)
                | ((bytes[1] & 0xFF) << 8)
                | ((bytes[2] & 0xFF) << 16)
                | ((bytes[3] & 0xFF) << 24);
    }

    public static byte[] int2byte(int i) {
        byte[] result = new byte[4];
        result[3] = (byte) ((i >> 24) & 0xFF);
        result[2] = (byte) ((i >> 16) & 0xFF);
        result[1] = (byte) ((i >> 8) & 0xFF);
        result[0] = (byte) (i & 0xFF);
        return result;
    }

    public static String _sendAndWait(ConnectObj connectObj, String message) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(EXECUTE, HANDLER_MESSAGE);
        jsonObject.put(MESSAGE, message);
        return connectObj.getTcpElementW().sendAndWait(jsonObject.toJSONString());
    }

    public static void _send(ConnectObj connectObj, String message) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(EXECUTE, HANDLER_MESSAGE);
        jsonObject.put(MESSAGE, message);
        connectObj.getTcpElementW().send(jsonObject.toJSONString());
    }
}
